package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qf.mapper.DtsGoodsSpecificationMapper;
import com.qf.pojo.DtsGoodsSpecification;
import com.qf.service.DtsGoodsSpecificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author 千锋教育
 * @Company http://www.mobiletrain.org/
 * @Version 1.0
 */
@Service
public class DtsGoodsSpecificationServiceImpl implements DtsGoodsSpecificationService {

    @Autowired
    DtsGoodsSpecificationMapper specificationMapper;

    @Override
    public void updateSpecifications(Integer goodsId, DtsGoodsSpecification[] specifications) {
        LambdaQueryWrapper<DtsGoodsSpecification> qw = new LambdaQueryWrapper<>();
        qw.eq(DtsGoodsSpecification::getGoodsId, goodsId);

        specificationMapper.delete(qw);


        if(specifications != null && specifications.length > 0) {
            for (DtsGoodsSpecification specification : specifications) {
                specification.setAddTime(new Date());
                specification.setUpdateTime(new Date());
                specification.setGoodsId(goodsId);
                specificationMapper.insert(specification);
            }
        }
    }

    @Override
    public void deleteByGoodsId(Integer goodId) {
        //条件是商品id，  修改的是状态
        //要修改的数据
        DtsGoodsSpecification specification = new DtsGoodsSpecification();
        specification.setDeleted(true);

        //条件
        LambdaQueryWrapper<DtsGoodsSpecification> qw = new LambdaQueryWrapper<>();
        qw.eq(DtsGoodsSpecification::getGoodsId, goodId);


        specificationMapper.update(specification, qw);


    }

    @Override
    public void addSpecification(Integer goodsId, DtsGoodsSpecification[] specifications) {
        if(specifications != null && specifications.length > 0) {
            for (DtsGoodsSpecification specification : specifications) {
                specification.setAddTime(new Date());
                specification.setUpdateTime(new Date());
                specification.setGoodsId(goodsId);
                specificationMapper.insert(specification);
            }
        }
    }
}
